package studyguide.ch07;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class AlertMidlet extends MIDlet {

	private final TextBox textBox;
	private final Command confirmCommand;
	private final Command clearCommand;
	private final Command exitCommand;
	private final Display display;
	
	private class SampleCommandListener implements CommandListener {

		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				String text = textBox.getString();
				System.out.println("Entered text:" + text);
				destroyApp(true);
				notifyDestroyed();
			} else if (command == confirmCommand) {
				Alert alert = new Alert(
						"Confirmation", 
						"Here's what you have typed:" + textBox.getString(), 
						null, 
						AlertType.INFO);
				alert.setTimeout(Alert.FOREVER);
				display.setCurrent(alert);
			} else if (command == clearCommand) {
				Alert confirm = new Alert(
						"Confirmation", 
						"Really clear all text?", 
						null, 
						AlertType.CONFIRMATION);
				
				final Command okCommand = new Command("Ok", Command.OK, 1);
				final Command cancelCommand = new Command("Cancel", Command.CANCEL, 1);
				confirm.addCommand(okCommand);
				confirm.addCommand(cancelCommand);
				
				confirm.setCommandListener(new CommandListener() {
					public void commandAction(Command c, Displayable d) {
						if (c == okCommand) {
							// Clear text and display main screen
							textBox.delete(0, textBox.getString().length());
							display.setCurrent(textBox);
						} else if (c == cancelCommand) {
							// Just go back to main screen
							display.setCurrent(textBox);
						} else {
							throw new RuntimeException("UNKNOWN COMMAND" + c);
						}
					}
					
				});
				
				display.setCurrent(confirm);
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
		
	}
	
	public AlertMidlet() {
		textBox = new TextBox(
				"TextBox Sample",
				"Text sample. ",
				500,
				TextField.ANY 
				 | TextField.INITIAL_CAPS_SENTENCE
				 | TextField.SENSITIVE);
		exitCommand = new Command("Exit", Command.EXIT, 1);
		confirmCommand = new Command("Confirm", Command.OK, 1);
		clearCommand = new Command("Clear All", Command.OK, 1);
		
		textBox.addCommand(exitCommand);
		textBox.addCommand(confirmCommand);
		textBox.addCommand(clearCommand);
		SampleCommandListener listener = new SampleCommandListener();
		textBox.setCommandListener(listener);
		display = Display.getDisplay(this);
	}
	
	protected void destroyApp(boolean unconditional) {
		// No Op
	}

	protected void pauseApp() {
		// No Op
	}

	protected void startApp() throws MIDletStateChangeException {
		System.out.println("Starting (or restarting) application");
		display.setCurrent(textBox);
	}

}
